package com.spic.business.plan.report.controller;

import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.plan.report.domain.PlanGreatItemsReport;
import com.spic.business.plan.report.service.IPlanGreatItemsReportService;
import com.spic.common.core.utils.StringUtils;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.api.RemoteDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 重大事项报告Controller
 *
 * @author spic
 * @date 2021-10-15
 */
@RestController
@RequestMapping("plan/report")
public class PlanGreatItemsReportController extends BaseController
{
    @Autowired
    private IPlanGreatItemsReportService planGreatItemsReportService;
    @Autowired
    private RemoteDeptService remoteDeptService;
    /**
     * 查询重大事项报告列表
     */
    @PreAuthorize(hasPermi = "business/plan:report:list")
    @GetMapping("/list")
    public TableDataInfo list(PlanGreatItemsReport planGreatItemsReport)
    {
        //获取部门及子部门
        if (StringUtils.isNotEmpty(planGreatItemsReport.getSubmitDepartment())){
            List<Long> submitDepartments = remoteDeptService.getDeptAndChildren(Long.parseLong(planGreatItemsReport.getSubmitDepartment())).getData();
            planGreatItemsReport.setSubmitDepartments(submitDepartments);
        }
        startPage();
        List<PlanGreatItemsReport> list = planGreatItemsReportService.selectPlanGreatItemsReportList(planGreatItemsReport);
        return getDataTable(list);
    }

    /**
     * 导出重大事项报告列表
     */
    @PreAuthorize(hasPermi = "business/plan:report:export")
    @Log(title = "重大事项报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlanGreatItemsReport planGreatItemsReport) throws IOException
    {
        List<PlanGreatItemsReport> list = planGreatItemsReportService.selectPlanGreatItemsReportList(planGreatItemsReport);
        ExcelUtil<PlanGreatItemsReport> util = new BusinessExcelUtil<PlanGreatItemsReport>(PlanGreatItemsReport.class);
        util.exportExcel(response, list, "重大事项报告数据");
    }

    /**
     * 获取重大事项报告详细信息
     */
    @PreAuthorize(hasPermi = "business/plan:report:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(planGreatItemsReportService.selectPlanGreatItemsReportById(id));
    }

    /**
     * 新增重大事项报告
     */
    @PreAuthorize(hasPermi = "business/plan:report:add")
    @Log(title = "重大事项报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlanGreatItemsReport planGreatItemsReport)
    {
        return toAjax(planGreatItemsReportService.insertPlanGreatItemsReport(planGreatItemsReport));
    }

    /**
     * 修改重大事项报告
     */
    @PreAuthorize(hasPermi = "business/plan:report:edit")
    @Log(title = "重大事项报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlanGreatItemsReport planGreatItemsReport)
    {
        return toAjax(planGreatItemsReportService.updatePlanGreatItemsReport(planGreatItemsReport));
    }

    /**
     * 删除重大事项报告
     */
    @PreAuthorize(hasPermi = "business/plan:report:remove")
    @Log(title = "重大事项报告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(planGreatItemsReportService.deletePlanGreatItemsReportByIds(ids));
    }
}
